@import org.silkframework.workspace.Constants
@import org.silkframework.dataset.Dataset
@import org.silkframework.workspace.User
@import controllers.rules.routes.Assets
@import org.silkframework.rule.TransformSpec

@(project: String, task: String)

@datasets = @{ User().workspace.project(project).tasks[Dataset].toSeq }

@transformationTask = @{ User().workspace.project(project).tasks[TransformSpec].find(_.id == task) }

@currentInput = @{ transformationTask.map(_.data.selection) }

@currentOutput = @{ transformationTask.flatMap(_.data.outputs.headOption) }

@prefixes = @{ User().workspace.project(project).config.prefixes }

@widgets.dialog(title = "Transformation Task") {
<div class="dialog-form" id="transFormationDialogForm">
  @if(task.isEmpty) {
    <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
      <input class="mdl-textfield__input" type="text" id="transformation_task_name" name="transformation_task_name"/>
      <label class="mdl-textfield__label" for="transformation_task_name">Name</label>
    </div>
  }
  @if(currentInput.isDefined) {
    @widgets.datasetSelect(project, "source", Some(currentInput.get.inputId))
  } else {
    @widgets.datasetSelect(project, "source", None)
  }
  <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
    <input class="mdl-textfield__input" type="text" id="transformation_type" name="transformation_type" @for(t <- transformationTask) { value="@t.data.selection.typeUri.serialize(prefixes)" } />
    <label class="mdl-textfield__label" for="transformation_type">Source Type</label>
    <div class="mdl-tooltip mdl-tooltip--right" for="transformation_type">
      The type of entities to be retrieved from the source dataset.
    </div>
  </div>
  @widgets.restriction("source", currentInput.map(_.restriction.serialize).getOrElse(""))
  <div class="mdl-selectfield mdl-js-selectfield mdl-selectfield--floating-label">
    <select class="mdl-selectfield__select" id="output_dataset" name="output_dataset">
      <option value=""></option>
      @for(dataset <- datasets) {
        @if(currentOutput.isDefined && dataset.id == currentOutput.get) {
          <option value="@dataset.id" selected="selected">@dataset.id</option>
        } else {
          <option value="@dataset.id">@dataset.id</option>
        }
      }
    </select>
    <label class="mdl-selectfield__label" for="output_dataset">Output</label>
    <div class="mdl-tooltip mdl-tooltip--right" for="target_vocabularies">
      Transformed entities are written to this dataset.
    </div>
  </div>
  <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
    <input class="mdl-textfield__input" type="text" id="target_vocabularies" name="target_vocabularies" @for(t <- transformationTask) { value="@t.targetVocabularies.mkString(", ")" } />
    <label class="mdl-textfield__label" for="target_vocabularies">Target Vocabularies</label>
    <div class="mdl-tooltip mdl-tooltip--right" for="target_vocabularies">
      Comma-separated list of target vocabularies.
    </div>
  </div>
</div>
}

<script type="text/javascript">
  // The variable name.
  var varName;
  // The name of the currently edited datasource.
  var sourceName;

  $(function() {
    // Add autocompletion
    $("input[name='transformation_type']").autocomplete({
      appendTo: '#transFormationDialogForm',
      source: function( request, response ) {
        var dataset = $("select[name='source_dataset']").val();
        $.get('@config.baseUrl/workspace/datasets/@project/' + dataset + '/types', { search: request.term }, function(data) {
          response(data);
        });
      },
      minLength: 0,
      close: function(event, ui) {
        $("#transformation_type").parent().addClass("is-dirty");
      }
    }).focus(function() { $(this).autocomplete("search"); });

  });

  function submit() {
    var values = {
      name: @if(!task.isEmpty) {'@task'} else {$("#transformation_task_name").val()},
      source: $("#source_dataset").val(),
      sourceType: $("#transformation_type").val(),
      restriction: $("#source_restriction_textarea").val(),
      output: $("#output_dataset").val(),
      targetVocabularies: $("#target_vocabularies").val()
    };

    if(values.name.length === 0) {
      alert("Task name is empty.");
      return;
    }

    $.ajax({
      type: 'PUT',
      url: '@config.baseUrl/transform/tasks/@project/' + values.name,
      data: values,
      success: function(data) {
        closeDialog();
        reloadWorkspace();
      },
      error: function(request) {
        alert(request.responseText);
      }
    });
  }
</script>
